<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="servlets.message.*"%>
<%@ page import="org.hibernate.SessionFactory"%>
<%@ page import="org.hibernate.Session"%>
<%@ page import="java.util.List"%>
<%@ page import="hibernate.model.*"%>
<%@ page import="hibernate.util.HibernateUtil"%>
<%@ page import="hibernate.method.*"%>
<script>
$(document).ready(function() {

$('#local').change(function(event) {
        var local_id = $("select#local").val();
        $.get('../deleteTable', {
                local_id : local_id
        },  function(response) {
            console.log(response);
            var select = $('#table');
            select.find('option').remove();
              $.each(response, function(index, value) {
                  if (value.trim() != ""){
            	  $.ajax({
           	       url: "../tableDetails",
           	       type: "GET",//type of posting the data
           	       data: {
           		       table_id: value
           	       },
           	       dataType: 'json',
           	       success: function (json) {
           			var data = json;
           			var smoking;
           			if (json.smokingTable == "true") smoking = "dla palących";
           			else smoking = "dla niepalących";
              $('<option>').val(value).text(json.labelTable + " , dla " + json.personsTable + " osób, " + smoking).appendTo(select);
           	    },
     	       error: function(xhr, ajaxOptions, thrownError){
     	       },
     	       timeout : 15000
     	  });
                  }
              });
              
        });
});
});
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<div class="page-header">
	<h2 class="page-title">USUŃ STOLIK</h2>
</div>

<br />
<input type="hidden" id="hiddenField"/>
<div id="content">
<%
SessionFactory sessionFactory = HibernateUtil
.getSessionAnnotationFactory();
Session s = sessionFactory.openSession();
s.beginTransaction();
LocalMethods lm = new LocalMethods();

List<Local> lmlist = lm.listAllLocals(sessionFactory, s); 
s.getTransaction().commit();
%>

<form action="../deleteTable" method="POST" name='deleteTable'>
		<table>
			<tr>
				<td>Wybierz lokal:</td>
				<td>
				<select id="local" name="local" style="width:250px">
						
						<%		if (lmlist.size() > 0) {
							for (Local l1 : lmlist) {
								%>
						<option value="<%=l1.getLocal_id()%>"><%=l1.getCity() +", "+ l1.getStreet() + " " + l1.getLocal_number()%></option>
						<%}
							
						}
						if (lmlist.size() == 0) {
					out.println("Nie ma zadnych lokali w bazie");
					
						} %>
						</select>
				</td>
			</tr>
			<tr>
				<td>Wybierz stolik:</td>
				<td>
				
				<select id="table" name="table">
						<option value="0" selected>Wybierz stolik</option>
						</select>
				</td>
			</tr>
			<tr>
			<td>
			
			<input type="submit" class="mainBtn" value="Stworz stolik" />
			</td>
			</tr>
			
			</table>
	</form>


</div>